package com.piece.core.framework.support.bootstrap;

import lombok.Data;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Bootstrap DataTable 对象
 */
@Data
public class DataTable<T> implements Serializable {

    // 当前页数
    private int start = 0;

    // 每页记录数量
    private int length = 0;

    // 总记录数
    private int recordsTotal;

    // 过滤后记录数
    private int recordsFiltered;

    // 请求次数
    private int draw = 0;

    // 结果集
    private List<T> data = new ArrayList();

    // 额外信息
    private String extraInfo;

    public DataTable() {
        super();
    }

    public DataTable(QueryTable query) {
        if (query != null) {
            this.start = query.getStart() < 0 ? 0 : query.getStart();
            this.length = query.getLength() < 0 ? 0 : query.getLength();
        }
    }

    public DataTable(QueryTable query, int recordsTotal) {
        if (query != null) {
            this.start = query.getStart() < 0 ? 0 : query.getStart();
            this.length = query.getLength() < 0 ? 0 : query.getLength();
        }
        this.recordsTotal = recordsTotal;
        this.recordsFiltered = recordsTotal;
    }

    public DataTable(int start, int length, int recordsTotal) {
        this.start = start;
        this.length = length;
        this.recordsTotal = recordsTotal;
        this.recordsFiltered = recordsTotal;
    }

    public DataTable(int start, int length, List<T> data, int recordsTotal) {
        this.start = start;
        this.length = length;
        this.data = data;
        this.recordsTotal = recordsTotal;
        this.recordsFiltered = recordsTotal;
    }

    public void setRecordsTotal(int recordsTotal) {
        this.recordsTotal = recordsTotal;
        this.recordsFiltered = recordsTotal;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.reflectionToString(this);
    }
}